package sjtu.ist.warehouse4ws.action;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.json.annotations.JSON;

import sjtu.ist.warehouse4ws.model.ShiroUser;
import sjtu.ist.warehouse4ws.service.ShiroRoleService;
import sjtu.ist.warehouse4ws.service.ShiroUserService;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	private List<ShiroUser> userList = new ArrayList<ShiroUser>();
	private ShiroUserService shiroUserService;
	private ShiroRoleService shiroRoleService;
	private ShiroUser user;
	private boolean success;
	private String message;
	
	private String userName;
	private String pw1;
	private String pw2;
	
	private Integer id;
	
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	
	
	@JSON(name = "totalUser")
	public int getTotalUserList() {
		return this.userList.size();
	}
	
	@JSON(name = "userList")
	public List<ShiroUser> getUserList() {
		return userList;
	}

	public void setShiroUserService(ShiroUserService shiroUserService) {
		this.shiroUserService = shiroUserService;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@JSON(name = "message")
	public String getMessage() {
		return message;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	@JSON(name = "success")
	public boolean isSuccess() {
		return success;
	}

//	@JSON(name = "isUserNameAdd")
//	public boolean isUserNameAdd() {
//		return userService.isUserNameAdd(userName, password);
//	}

	
	
	
	//@JSON(name = "companyUser")
	public String showUserList(){		
		Subject currentUser = SecurityUtils.getSubject();
		Integer companyId=(Integer)currentUser.getSession().getAttribute("companyId");
		this.userList = shiroUserService.getUserList(companyId,"user");
		success=true;
		return SUCCESS;
	}
	
	public String addCompanyUser(){
	
		Subject currentUser = SecurityUtils.getSubject();
		Integer companyId=(Integer)currentUser.getSession().getAttribute("companyId");
		
		String str1="用户名已经被添加，请重新选择用户名";
		String str2="用户添加成功";
		setSuccess(true);
		if(shiroUserService.findUser(getUserName())!=null){
			setMessage(str1);
			return SUCCESS;
		}
		user = new ShiroUser();
		user.setUsername(userName);
		user.setPassword(pw1);
		user.setShiroRoles(new HashSet(shiroRoleService.findByName("user")));
		user.setCompanyId(companyId);
		shiroUserService.saveShiroUser(user);
		setMessage(str2);
		return SUCCESS;
	}

	public String deleteCompanyUser(){	
		shiroUserService.delete(id);
		return SUCCESS;
	}

	public void setPw1(String pw1) {
		this.pw1 = pw1;
	}

	public String getPw1() {
		return pw1;
	}

	public void setPw2(String pw2) {
		this.pw2 = pw2;
	}

	public String getPw2() {
		return pw2;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public void setShiroRoleService(ShiroRoleService shiroRoleService) {
		this.shiroRoleService = shiroRoleService;
	}
}
